/**
 * Created by DevilsEyes on 2016/3/14.
 */
//这是一个工具包
'use strict';

define(['jquery', 'layer', 'fakeJSON'], function ($, layer, fakeJSON) {
    $ = window.$;

    var ex = {
        noop: function () {
            //强迫症函数
        },
        jsonp: function (option) {
            if (!(option.url && option.success)) {
                throw 'JSONP请求参数不足';
            }
            option.method = option.method || 'GET';
            option.data = option.data || {};
            option.error = option.error || ex.noop;
            option.loading = option.loading || true;
            option.before = option.before || ex.noop;

            var FakeJSON_PATH = fakeJSON.getFakePath(option.url);
            if (!FakeJSON_PATH) {
                $.ajax({
                    url: option.url + '?_method=' + option.method,
                    dataType: 'jsonp',
                    data: option.data,

                    success: function (res) {
                        console.log(res);

                        if (typeof res == 'string') {
                            res = JSON.parse(res);
                        }

                        if (res.code != 0 && res.code != 200) {
                            return this.error(res);
                        }
                        option.success(res);
                    },

                    beforeSend: function () {
                        if (option.loading)layer.load(2);
                        option.before && option.before()
                    },
                    complete: function () {
                        if (option.loading)layer.closeAll('loading');
                        option.after && option.after()
                    },
                    error: option.error
                });
            } else {
                $.ajax({
                    url: FakeJSON_PATH,
                    success: function (res) {
                        //console.log(option);

                        if (typeof res == 'string') {
                            res = JSON.parse(res);
                        }

                        if (res.code != 0 && res.code != 200) {
                            return this.error(res);
                        }
                        option.success(res);
                    },

                    beforeSend: function () {
                        if (option.loading)layer.load(2);
                        option.before && option.before()
                    },
                    complete: function () {
                        if (option.loading)layer.closeAll('loading');
                        option.after && option.after()
                    },
                    error: option.error
                });
            }

        },

        //强行异步
        async: function (callback, time) {
            return setTimeout(callback, time || 0);
        },
        //拷贝对象
        copy: function (obj) {
            if (typeof obj == 'object') {
                var o = obj instanceof Array ? [] : {};
                for (var i in obj) {
                    o[i] = ex.copy(obj[i]);
                }
                return o;
            } else {
                return obj;
            }
        },
        //将图片转化为标准格式
        formatImage: function (object) {

            var success = object.success;//回调
            var error = object.error;//回调
            var url = object.url;
            var res = {};

            if (url === null) {
                res = {
                    url: '',
                    width: 0,
                    height: 0,
                    ratio: 0
                };
                success(res);
            }
            else if (url.match('_W_')) {
                try {
                    var str = url.split('_W_')[1];
                    var w = +str.split('X')[0];
                    var h = +str.split('X')[1];
                    res = {
                        url: url,
                        width: w,
                        height: h,
                        ratio: w / h
                    };
                    success(res);
                } catch (e) {
                    error ?
                        error(e)
                        :
                        function () {
                        };
                }
            } else {
                $.ajax({
                    url: url + '?imageInfo',
                    success: function (obj) {
                        var res = {
                            url: url,
                            width: obj.width,
                            height: obj.height,
                            ratio: obj.width / obj.height
                        };
                        success(res);
                    },
                    error: function (e) {
                        error ?
                            error(e)
                            :
                            function () {
                            };
                    }
                })
            }

        },
        params: function (obj, type) {
            var ws, br;
            switch (type) {
                case 'html':
                    ws = '&nbsp;';
                    br = '<br>';
                    break;
                case 'log':
                    ws = ' ';
                    br = '\n';
                    break;
            }

            function space(i) {
                if (i == 1)return ws;
                else {
                    return ws + space(i - 1);
                }
            }

            var str = '';
            if (typeof obj != 'object') {
                str += obj + space(3) + '[TYPE:' + typeof obj + ']';
            } else {
                for (var i in obj) {
                    str += space(1) + i + ': ' + obj[i] + br;

                    if (typeof obj[i] == 'object') {
                        for (var j in obj[i]) {
                            str += space(5) + '└─' + space(1) + j + ': ' + obj[i][j] + br;

                            if (typeof obj[i][j] == 'object') {
                                for (var k in obj[i][j]) {
                                    str += space(10) + '└─' + space(1) + k + ': ' + obj[i][j][k] + br;
                                }
                                str += br;
                            }
                        }
                        str += br;
                    }
                }
                str += br;
            }
            return str;
        },
        formatTime: function (time, format) {

            time = +time;
            var h, m, s, S;
            S = time % 1000;
            time = Math.floor(time / 1000);
            s = time % 60;
            time = Math.floor(time / 60);
            m = time % 60;
            time = Math.floor(time / 60);
            h = time % 60;

            var map = {
                "h": h, //小时
                "m": m, //分
                "s": s, //秒
                "S": S //毫秒
            };
            format = format.replace(/([hmsS])+/g, function (all, t) {
                var v = map[t];
                if (v !== undefined) {
                    if (all.length > 1) {
                        v = '0' + v;
                        v = v.substr(v.length - 2);
                    }
                    return v;
                }
                return all;
            });
            return format;
        }
    };

    return ex;

});